﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Clipping.Infrastructure.Models;
using System.Collections.ObjectModel;
namespace ClippingEditor
{
    /// <summary>
    /// Interaction logic for Dlg_SelectCategories.xaml
    /// </summary>
    public partial class Dlg_SelectCategories : Window
    {
        List<ClientInfo> clients = null;
        ObservableCollection<ClassInfo> selectedCategories = null;

        public Dlg_SelectCategories()
        {
            InitializeComponent();
        }
        public Dlg_SelectCategories(List<ClientInfo> clients, List<ClassInfo> selectedCategories)
            : this()
        {

            this.clients = clients;
            if (selectedCategories != null)
                this.selectedCategories = new ObservableCollection<ClassInfo>(selectedCategories);
            else
                this.selectedCategories = new ObservableCollection<ClassInfo>();
            this.Loaded += new RoutedEventHandler(Dlg_SelectCategories_Loaded);
        }

        void Dlg_SelectCategories_Loaded(object sender, RoutedEventArgs e)
        {
            this.cbx_Clients.ItemsSource = this.clients;
            this.lbx_SelectedCategories.ItemsSource = selectedCategories;

            if (selectedCategories != null && selectedCategories.Count > 0)
            {
                this.cbx_Clients.SelectedItem = selectedCategories.First().Client;
            }
        }

        public List<Clipping.Infrastructure.Models.ClassInfo> SelectedCategories
        {
            get;
            set;
        }

        private void btn_Add_Click(object sender, RoutedEventArgs e)
        {
            ClassInfo selectedClass = this.tv_Categories.SelectedItem as ClassInfo;
            addCategory(selectedClass);
        }

        private void addCategory(ClassInfo selectedClass)
        {
            if (!this.selectedCategories.Contains(selectedClass))
            {
                if (this.selectedCategories.Count > 0 && this.selectedCategories.First().Client != selectedClass.Client)
                    this.selectedCategories.Clear();


                this.selectedCategories.Add(selectedClass);
            }
        }

        private void btn_Remove_Click(object sender, RoutedEventArgs e)
        {
            ClassInfo classInfo = this.lbx_SelectedCategories.SelectedItem as ClassInfo;
            this.selectedCategories.Remove(classInfo);
        }

        private void btn_Save_Click(object sender, RoutedEventArgs e)
        {
            this.SelectedCategories = this.selectedCategories.ToList();
            this.DialogResult = true;
            this.Close();
        }

        private void btn_Cancel_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
            this.Close();
        }

        private void cbx_Clients_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ClientInfo selectedClient = this.cbx_Clients.SelectedItem as ClientInfo;
            if (selectedClient != null)
            {
                this.tv_Categories.ItemsSource = selectedClient.Classes;
            }
        }

        private void tv_Categories_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            ClassInfo ci = this.tv_Categories.SelectedItem as ClassInfo;
            if (ci != null && (ci.SubClasses == null || ci.SubClasses.Count == 0))
            {
                this.btn_Add.IsEnabled = true;
            }
            else
            {
                this.btn_Add.IsEnabled = false;
            }
        }

        private void lbx_SelectedCategories_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (this.lbx_SelectedCategories.SelectedItem != null)
            {
                ClassInfo classInfo = this.lbx_SelectedCategories.SelectedItem as ClassInfo;
                if (classInfo != null)
                    this.btn_Remove.IsEnabled = true;
                else
                    this.btn_Remove.IsEnabled = false;
            }
            else
                this.btn_Remove.IsEnabled = false;
        }

        private void TreeViewItem_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            TreeViewItem tvi = e.Source as TreeViewItem;
            if (tvi != null)
            {
                tvi.IsExpanded = true;

                ClassInfo ci = tvi.DataContext as ClassInfo;
                if (ci != null && (ci.SubClasses == null || ci.SubClasses.Count == 0))
                {
                    addCategory(ci);
                }
            }
        }
        private void ListBoxItem_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            ListBoxItem lbi = sender as ListBoxItem;
            if (lbi != null)
            {
                ClassInfo ci = lbi.DataContext as ClassInfo;
                if (ci != null)
                {
                    this.selectedCategories.Remove(ci);
                }
            }
        }

    }
}
